package com.css.crm.ws.customer;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.css.crm.service.CustomerService;
import com.css.crm.service.impl.CustomerServiceImpl;
import com.css.crm.utility.WebserviceUtil;
import com.css.crm.vo.CustomerVo;
import com.css.db.MyBatisConfig;

@Path(value = "/createCustomer")
public class CreateCustomer {

	protected Logger log = Logger.getLogger(this.getClass().toString());

	@POST
	@Produces(MediaType.TEXT_PLAIN)
	public boolean createCustomer(String customerXml) {
		SqlSession session = null;
		try {
			CustomerVo customer = WebserviceUtil.unMarshal(customerXml, CustomerVo.class);
			CustomerService customerService = new CustomerServiceImpl();
			session = MyBatisConfig.getSession();
			customerService.setSession(session);
			customerService.createCustomer(customer);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return false;
		} finally {
			session.close();
		}
		return true;
	}

}